915. Rectangular or not?

 

The lengths of the sides of a triangle are given. Determine whether the triangle is right-angled.

 

Input. Three positive integers a, b, c – the lengths of the triangle's sides, each not exceeding 1000.

 

Output. Print YES if the triangle is right-angled, and NOotherwise.

 

Sample input 1

Sample output 1

3 5 4

YES

 

 

Sample input 2

Sample output 2

3 5 5

NO

 

 

SOLUTION

conditional statement

 

Algorithm analysis

A triangle with sides a, b, and c is a right triangle if one of its sides is the hypotenuse and the other two are the legs. According to the Pythagorean theorem, the square of the hypotenuse’s length equals the sum of the squares of the lengths of the legs. This means that one of the following conditions must hold:

a2 = b2 + c2, b2 = a2 + c2 or c2 = a2 + b2,

where a, b and c can be the sides of the triangle in any order.

 

Algorithm implementation

Read the lengths of the triangle’s sides.

 

scanf("%d %d %d",&a,&b,&c);

 

Check whether the triangle is right-angled and print the corresponding result.

 

if ((a * a + b * b == c * c) ||

    (a * a + c * c == b * b) ||

    (b * b + c * c == a * a))

  puts("YES");

else

  puts("NO");

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    int b = con.nextInt();

    int c = con.nextInt();

   

    if ((a * a + b * b == c * c) ||

        (a * a + c * c == b * b) ||

        (b * b + c * c == a * a))

      System.out.println("YES");

    else

      System.out.println("NO");

    con.close();

  }

}  

 

Python implementation

Read the lengths of the triangle’s sides.

 

a, b, c = map(int,input().split())

 

Check whether the triangle is right-angled and print the corresponding result.

 

if a * a + b * b == c * c or a * a + c * c == b * b

                          or b * b + c * c == a * a:

  print("YES")

else:

  print("NO")